package backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class letterCombinations {
    List<String> result = new ArrayList<>();
    String[] letterMap = new String[]{
            "", // 0
            "", // 1
            "abc", // 2
            "def", // 3
            "ghi", // 4
            "jkl", // 5
            "mno", // 6
            "pqrs", // 7
            "tuv", // 8
            "wxyz", // 9
    };
    StringBuilder path = new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if (digits.isEmpty()){
            return result;
        }
        findCombination(digits, 0);
        return result;
    }
    public void findCombination(String digits, int num){
        if (num == digits.length()){
            result.add(path.toString());
            return;
        }
        String str = letterMap[digits.charAt(num) - '0'];
        for (int i = 0; i < str.length(); i++) {
            path.append(str.charAt(i));
            findCombination(digits, num + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
